| Conditions | 2 |
| Paths | 8 |
| Total Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var http = require('http'),
|
||
| 138 | app.get('/query/', function (req, res, next) {
|
||
| 139 | if (program.fulllog) {
|
||
| 140 | var start = new Date(); |
||
| 141 | console.log((timeStamp() + 'Started to proceed: ').cyan + req.query.id.yellow); |
||
|
1 ignored issue
–
show
|
|||
| 142 | } |
||
| 143 | login(req.query.id, req.query.pwd, res, function (headers, iires) {
|
||
| 144 | var ret = {};
|
||
| 145 | var $ = cheerio.load(iires.text); |
||
| 146 | |||
| 147 | // 进入成绩页面 |
||
| 148 | superagent.get(base + $('li[title="我的成绩"] a').attr('href'))
|
||
| 149 | .set(headers) |
||
| 150 | .end(function (err, iiires) {
|
||
| 151 | if (err) {
|
||
| 152 | console.log((timeStamp() + 'Fail to obtain grades\n' + err.stack).red); |
||
|
1 ignored issue
–
show
|
|||
| 153 | ret.error = '获取成绩失败'; |
||
| 154 | return next(err); |
||
| 155 | } |
||
| 156 | program.fulllog && console.log((timeStamp() + 'Successfully entered grades page.').green); |
||
| 157 | |||
| 158 | $ = cheerio.load(iiires.text); |
||
| 159 | |||
| 160 | // 获取成绩列表 |
||
| 161 | let grades = {};
|
||
| 162 | let failed = {};
|
||
| 163 | $('#dataList').each(function (index) {
|
||
| 164 | // cheerio没有实现jQuery的lt |
||
| 165 | if (index >= 2) |
||
| 166 | return; |
||
| 167 | $(this).find('tr[class!="theadCss"]').each(function() {
|
||
| 168 | // 这段写得真是要吐血了 |
||
| 169 | let subject = escaper.unescape($(this).find('td[align="left"]').eq(1).text());
|
||
| 170 | if (subject) {
|
||
| 171 | let score = $(this).find('font');
|
||
| 172 | if (score.text()) |
||
| 173 | grades[subject] = score.text(); |
||
| 174 | if (score.css('color'))
|
||
| 175 | failed[subject] = score.text(); |
||
| 176 | } |
||
| 177 | }); |
||
| 178 | }); |
||
| 179 | ret.grades = grades; |
||
| 180 | ret['subject-count'] = Object.getOwnPropertyNames(grades).length; |
||
| 181 | ret.failed = failed; |
||
| 182 | ret['failed-count'] = Object.getOwnPropertyNames(failed).length; |
||
| 183 | |||
| 184 | // 完成所有工作后,登出 |
||
| 185 | superagent.get(getRandomUrl(base + '/jsxsd/xk/LoginToXk?method=exit')) |
||
| 186 | .set(headers) |
||
| 187 | .end(function (err, iiiires) {
|
||
| 188 | if (err) {
|
||
| 189 | console.log((timeStamp() + 'Fail to logout\n' + err.stack).red); |
||
|
1 ignored issue
–
show
|
|||
| 190 | res.send({ error: '操作失败' });
|
||
| 191 | return next(err); |
||
| 192 | } |
||
| 193 | |||
| 194 | // 第五步:返回JSON |
||
| 195 | res.send(JSON.stringify(ret)); |
||
| 196 | program.fulllog && console.log((timeStamp() + 'Successfully logged out: ').green + req.query.id.yellow + (' (total time: ' + (new Date() - start) + 'ms)').green);
|
||
| 197 | }); |
||
| 198 | }); |
||
| 199 | }); |
||
| 200 | }); |
||
| 201 | |||
| 203 | console.log((timeStamp() + 'The server is now running on port ' + port + '.').green); |